//
//  AppDelegate.m
//  LastTemp
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "AppDelegate.h"
#import "HomePageViewController.h"
#import "musicViewController.h"
#import "MyViewController.h"
#import "SearchViewController.h"
#import "UMSocial.h"
#import "bofangViewController.h"
#import "AVSingleton.h"
#import "WelcomViewController.h"
#import "AFNetworkReachabilityManager.h"
#import "NoWifiViewController.h"



@interface AppDelegate ()<UITabBarControllerDelegate>
@property(nonatomic,retain)UIView *barView;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    [AVSingleton shareSingleton].internetNum = 10;
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
//
    [AVSingleton shareSingleton].internetNum2 = 1;
    
    // 设置会话类型
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    
    // 激活
    [session setActive:YES error:nil];
    
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    
    // 2.设置网络状态改变后的处理
    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        // 当网络状态改变了, 就会调用这个block
        switch (status) {
            case AFNetworkReachabilityStatusUnknown: // 未知网络
                NSLog(@"未知网络");
                if ([AVSingleton shareSingleton].internetNum3 == 1) {
                    [self goAp];
                }
                [AVSingleton shareSingleton].internetNum = 2;
                [AVSingleton shareSingleton].internetNum1 = 0;
                break;
                
            case AFNetworkReachabilityStatusNotReachable: // 没有网络(断网)
                NSLog(@"没有网络(断网)");
//                if (![AVSingleton shareSingleton].internetNum==10) {
                     [AVSingleton shareSingleton].internetNum = 1;
                
                [AVSingleton shareSingleton].internetNum3 = 1;
               
                
//                NoWifiViewController *wifi = [[NoWifiViewController alloc] init];
//                self.window.rootViewController = wifi;
//                [wifi release];

                break;
                
            case AFNetworkReachabilityStatusReachableViaWWAN: // 手机自带网络
                NSLog(@"手机自带网络");

                if ([AVSingleton shareSingleton].internetNum3 == 1) {
                    [self goAp];
                }
                [AVSingleton shareSingleton].internetNum = 2;
                [AVSingleton shareSingleton].internetNum1 = 1;
               
               
                break;
                
            case AFNetworkReachabilityStatusReachableViaWiFi: // WIFI
                NSLog(@"WIFI");
                if ([AVSingleton shareSingleton].internetNum3 == 1) {
                    [self addApp];
                }
                [AVSingleton shareSingleton].internetNum = 2;
                [AVSingleton shareSingleton].internetNum1 = 2;
                
                break;
        }
    }];
    
    // 3.开始监控
    [mgr startMonitoring];
    
 
      [AVSingleton shareSingleton].playNum3 = 1;
    [AVSingleton shareSingleton].delegateApp = self;
    
    if (![[NSUserDefaults standardUserDefaults]boolForKey:@"firstLaunch"]) {
        [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"firstLaunch"];
        NSLog(@"第一次启动");
        WelcomViewController *welcome = [[WelcomViewController alloc]init];
        self.window.rootViewController = welcome;
        [welcome release];
        
    }else
    {
        NSLog(@"不是第一次用了");
        [self performSelector:@selector(welcomeEnterPage) withObject:nil afterDelay:1.0f];
    }

    
    
    
    
    
    
 
    return YES;
}
- (void)dealloc
{
    [_barView release];
    [_window release];
    [super dealloc];
}

- (void)changAlphNight
{
    self.window.alpha = 0.5;
}

- (void)changAlphDay
{
    self.window.alpha = 1;
}


- (void)addAnimal
{
    UIView *animalView = [[UIView alloc] initWithFrame:CGRectMake(0, -11, 60, 60)];
    [self.barView addSubview:animalView];
    [animalView release];
    animalView.layer.cornerRadius = 30;
    animalView.backgroundColor = [UIColor whiteColor];
    // 小视图
    UIView *smallView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
    smallView.center = CGPointMake(30, 30);
    [animalView addSubview:smallView];
    smallView.layer.cornerRadius = 30;
    smallView.backgroundColor = [UIColor yellowColor];
    // 放图片
    UIImageView *animalImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
    [smallView addSubview:animalImageView];
    [animalImageView release];
    animalImageView.layer.cornerRadius = 30;
    animalImageView.backgroundColor = [UIColor cyanColor];
    animalImageView.image = [UIImage imageNamed:@"playButton"];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [smallView addSubview:button];
    button.frame = CGRectMake(0, 0, 60, 60);
    button.layer.cornerRadius = 30;
    [button addTarget:self action:@selector(presentClick:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)goAp
{
    [self addApp];
}
- (void)presentClick:(UIButton *)button
{
    
    [[AVSingleton shareSingleton] goPlyer];
    [AVSingleton shareSingleton].playNum = 0;
     [AVSingleton shareSingleton].playNum2 = 0;
  
    [AVSingleton shareSingleton].playNum3++;
    
    [AVSingleton shareSingleton].bigButton = [AVSingleton shareSingleton].time;
    
    
    


}

- (void)welcomeEnterPage
{
    UIImageView *welcomeImageView = [[UIImageView alloc] initWithFrame:self.window.bounds];
    [self.window addSubview:welcomeImageView];
    [welcomeImageView release];

    welcomeImageView.image = [UIImage imageNamed:@"welcom"];
    welcomeImageView.alpha = 1.0;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, self.window.frame.size.height/2,self.window.frame.size.width-50 , 60)];
    [welcomeImageView addSubview:label];
    [label release];
    label.text = @"只因为在人群中多看了你一眼,\n       再也没能忘掉你的容颜!";
    label.numberOfLines = 2;
    [label sizeToFit];
    
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(self.window.frame.size.width/3*2, self.window.frame.size.height/2+80,self.window.frame.size.width/2 , 30)];
    [welcomeImageView addSubview:label2];
    [label2 release];
    label2.text = @" ------王菲";
    
//    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.window.frame.size.width/2-50, 200, 100, 100)];
//    [welcomeImageView addSubview:imageView];
//    [imageView release];
//    imageView.image = [UIImage imageNamed:@"tubiao180"];
    
    [UIView animateWithDuration:2.5f delay:1.0f options:UIViewAnimationOptionCurveLinear
                     animations:^
     {
         welcomeImageView.alpha = 0.4;
     }
                     completion:^(BOOL finished)
     {
         [NSThread sleepForTimeInterval:0.3f];
         [welcomeImageView removeFromSuperview];
         
         
         
         [AVSingleton shareSingleton].delegate = self;;
         [UMSocialData setAppKey:@"553afad967e58e7cba0031e4"];
         HomePageViewController *home = [[HomePageViewController alloc] init];
         UINavigationController *naHome = [[UINavigationController alloc] initWithRootViewController:home];
         //    naHome.navigationBar.translucent = NO;
         
         home.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"bar5"] tag:1000];
         
         MyViewController *my = [[MyViewController alloc] init];
         UINavigationController *naMy = [[UINavigationController alloc] initWithRootViewController:my];
         //    naMy.navigationBar.translucent = NO;
         my.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"bar3"] tag:1001];
         
         SearchViewController *search = [[SearchViewController alloc] init];
         UINavigationController *naSearch = [[UINavigationController alloc] initWithRootViewController:search];
         //    naSearch.navigationBar.translucent = NO;
         //
         bofangViewController *play = [[bofangViewController alloc] init];
         UINavigationController *naPlay = [[UINavigationController alloc] initWithRootViewController:play];
         play.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"bofang" image:nil tag:1002];
         
         
         search.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"搜索" image:[UIImage imageNamed:@"bar1"] tag:1003];
         
         musicViewController *music = [[musicViewController alloc] init];
         UINavigationController *naMusic = [[UINavigationController alloc] initWithRootViewController:music];
         //    naMusic.navigationBar.translucent = NO;
         music.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"音乐库" image:[UIImage imageNamed:@"bar4"] tag:1004];
         
         UITabBarController *tabBar = [[UITabBarController alloc] init];
         tabBar.tabBar.translucent = NO;
         tabBar.viewControllers = [NSArray arrayWithObjects:naHome,naMy,naPlay,naSearch,naMusic, nil];
         self.window.rootViewController = tabBar;
         //     添加动画效果
         
         self.barView = [[UIView alloc] initWithFrame:CGRectMake(self.window.frame.size.width/5*2, 0, self.window.frame.size.width/4, 49)];
         [tabBar.tabBar insertSubview:self.barView atIndex:3];
         [_barView release];
         self.barView.backgroundColor = [UIColor clearColor];
         [self addAnimal];
         
         tabBar.delegate = self;
    
    
     }];
    
}


- (void)addApp
{
    
    [AVSingleton shareSingleton].delegate = self;;
    [UMSocialData setAppKey:@"553afad967e58e7cba0031e4"];
    HomePageViewController *home = [[HomePageViewController alloc] init];
    UINavigationController *naHome = [[UINavigationController alloc] initWithRootViewController:home];
    //    naHome.navigationBar.translucent = NO;
    
    home.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"bar5"] tag:1000];
    
    MyViewController *my = [[MyViewController alloc] init];
    UINavigationController *naMy = [[UINavigationController alloc] initWithRootViewController:my];
    //    naMy.navigationBar.translucent = NO;
    my.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"bar3"] tag:1001];
    
    SearchViewController *search = [[SearchViewController alloc] init];
    UINavigationController *naSearch = [[UINavigationController alloc] initWithRootViewController:search];
    //    naSearch.navigationBar.translucent = NO;
    //
    bofangViewController *play = [[bofangViewController alloc] init];
    UINavigationController *naPlay = [[UINavigationController alloc] initWithRootViewController:play];
    play.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"" image:nil tag:1002];
    
    //
    search.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"搜索" image:[UIImage imageNamed:@"bar1"] tag:1003];
    
    musicViewController *music = [[musicViewController alloc] init];
    UINavigationController *naMusic = [[UINavigationController alloc] initWithRootViewController:music];
    //    naMusic.navigationBar.translucent = NO;
    music.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"音乐库" image:[UIImage imageNamed:@"bar4"] tag:1004];
    
    UITabBarController *tabBar = [[UITabBarController alloc] init];
    tabBar.tabBar.translucent = NO;
    tabBar.viewControllers = [NSArray arrayWithObjects:naHome,naMy,naPlay,naSearch,naMusic, nil];
    self.window.rootViewController = tabBar;
    //     添加动画效果
    
    self.barView = [[UIView alloc] initWithFrame:CGRectMake(self.window.frame.size.width/5*2, 0, self.window.frame.size.width/5, 49)];
    [tabBar.tabBar insertSubview:self.barView atIndex:3];
    [_barView release];
    self.barView.backgroundColor = [UIColor clearColor];
    [self addAnimal];
    
    tabBar.delegate = self;
    
    


}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
